<template>
  <!-- (WBS、危大工程)与模型绑定 -->
  <div class="modelBusinessBind-container">
    <BimModel
      :modelId="childInitData.modelId"
      :allowAddTreeNode="true"
      @add:model="handleAddWbsModel"
      @model:switch="visible.switchModel = !visible.switchModel"
    />
    <BindingTemplate class="bindingTemplate" ref="BindingTemplate" :selectLevel="bindSelectLevel" :bindType="bindType" :modelId="childInitData.modelId" />
    <SelectProjectInBim :visible="visible.selectProject" @confirm="confirmSelectProject" @cancel="visible.selectProject = false" @empty="visible.selectProject = true"/>
    <SwitchModel
      :default-value="childInitData.modelId"
      v-if="visible.switchModel"
      @cancel="visible.switchModel = false"
      @model:select="handleModelSelected"
    />
  </div>
</template>

<script>
import SelectProjectInBim from '@/components/BimIntegrate/SelectProjectInBim'
import SwitchModel from '@/components/BimIntegrate/SwitchModel'
import BimModel from '@/components/BimIntegrate'
import BindingTemplate from '@/components/BimIntegrate/components/BindingTemplate'
export default {
  components: {
    BimModel,
    BindingTemplate,
    SwitchModel,
    SelectProjectInBim,
  },
  data() {
    return {
      visible: {
        selectProject: false,
        switchModel: false,
      },
      childInitData: {},
    }
  },
  computed: {
    bindSelectLevel() {
      const bindType = this.$route.meta.bindType || 'wbs'
      return bindType === 'wbs' ? 4 : null
    },
    bindType(){
      return this.$route.meta.bindType || 'wbs'
    },
    projectId() {
      return this.$store.getters['bimInfo/GET_PROJECTID']
    },
  },
  created() {
    this.initPage()
  },
  methods: {
    confirmSelectProject() {
      this.visible.selectProject = false
      this.initPage()
    },
    initPage() {
      const mainModelId = sessionStorage.getItem('mainModel')
      if (mainModelId) {
        this.childInitData = {
          ...this.childInitData,
          modelId: mainModelId,
        }
      }
    },
    handleAddWbsModel(data) { // 调用子组件，添加WBS绑定的构件
      this.$refs.BindingTemplate.handleModelItem('add', 0, data)
    },
    handleModelSelected(modelId) {
      this.visible.switchModel = false
      this.childInitData = {
        ...this.childInitData,
        modelId,
      }
    },
  },
}
</script>

<style lang="less" scoped>
.modelBusinessBind-container {
  padding-bottom: 20px;
  .bindingTemplate {
    margin: 0 20px;
    width: calc(100% - 40px);
    border: 1px solid #cccccc;
    border-radius: 8px;
  }
}
</style>